<template>
    <view class="my-page">
        <van-toast id="van-toast" />
        <view class="container">
            <!-- 已登录 -->
            <view class="login" v-if="isAuth">
                <!-- <view class="get-number">
					
				</view> -->
                <view class="text-area">
                    <van-button type="info" @click="save" size="small">保存</van-button>
                </view>
                <view class="btn-box">
                    <van-button type='primary' @click="logout">退出</van-button>
                </view>
            </view>
            <!-- 未登录 -->
            <view class="not-login" v-if="!isAuth">
                <view class="btn-box">
                    <van-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type='primary'>登录</van-button>
                </view>
                <view class="btn-box">
                    <van-button @click="goSubPackagePage" type='primary'>分包跳转测试</van-button>
                </view>
                <view class="btn-box">
                    <van-button @click="toastFn" type='primary'>Toast</van-button>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import dayjs from "dayjs";
import Toast from 'wxcomponents/@vant/weapp/dist/toast/toast.js'
import { newWxAuthorUserApi, getSelfInfoApi } from "@api/user"

export default {
    data() {
        return {
            code: '',
            isAuth: false, // 是否授权登录
            isLogin: uni.getStorageSync("token") // 是否登录
        }
    },
    onShow() {
        this.getLogin()
    },
    onLoad() {
        console.log('今年是：' + dayjs().year())
        console.log('当前时间：' + dayjs().format('YYYY-MM-DD HH:MM:ss'))
    },
    methods: {
        getLogin() {
            uni.login({
                provider: 'weixin',
                success: loginInfo => {
                    let { code } = loginInfo
                    this.code = code
                }
            })
        },
        async getPhoneNumber(e) {
            let { encryptedData, iv } = e.detail
            let data = {
                js_code: this.code,
                mobile_encrypted_data: encryptedData,
                mobile_iv: iv
            }
            console.log(data);
            await newWxAuthorUserApi(data).then(res => {
                if (!res) return
                let { access_token } = res
                uni.setStorageSync('token', access_token)
                this.isAuth = true
            })
        },
        async getUserInfo() {
            let data = {
                access_token: uni.getStorageSync("token")
            }
            await getSelfInfoApi(data).then(res => {
                if (!res) return
                console.log(res);
            })
        },
        goSubPackagePage() {
            console.log('分包跳转...');
            uni.navigateTo({
                url: '/pagesA/pages/home/index'
            })
        },
        toastFn() {
            Toast('toast...')
        },
        save() {
            let token = uni.getStorageSync("token")
            if (token) {
                // Toast('登录了')
            }
            if (!token) {
                // Toast('未登录')
            }
        },
        logout() {
            this.isAuth = false
            uni.removeStorageSync("token")
            uni.switchTab({
                url: '/pages/home/index'
            })
        }
    }
}
</script>

<style lang="less">
.my-page {
    .container {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 20px;

        .login,
        .not-login {
            width: 100%;
        }
    }
}


.text-area {
    display: flex;
    justify-content: center;
    min-height: 300px;
}

.btn-box {
    width: 100%;
    margin-top: 20px;
}
</style>
